//
//  HOPOauthViewController.swift
//  WB01
//
//  Created by HOP on 15/12/18.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit

import SVProgressHUD

class HOPOauthViewController: UIViewController, UIWebViewDelegate {
/// WebView开始加载
    func webViewDidStartLoad(webView: UIWebView) {
        // 显示正在加载
        // showWithStatus会一直显示
        // showInfoWithStatus,过1s自动关闭
//        SVProgressHUD.showInfoWithStatus("正在加载", maskType: SVProgressHUDMaskType.Gradient)
        SVProgressHUD.showWithStatus("正在加载", maskType: SVProgressHUDMaskType.Gradient)
        
     }
    /// WebView加载完毕
    func webViewDidFinishLoad(webView: UIWebView) {
    // 关闭指示器
        SVProgressHUD.dismiss()
    }
    /// webView是否要去加载一个网址,返回true去加载, false不去加载
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
       let url = request.URL!.absoluteString
        print("url: \(url)")
        //只有点击 取消或授权,才会加载 授权回调地址,其他情况不会加载授权回调地址
        // 不是授权回调地址,直接加载
        if !url.hasPrefix(HOPNetworkTools.sharedInstance.redirect_uri)
        {
        return true
        
        }
        // 是授权回调地址,判断是取消还是授权
        // 点击取消: http://www.baidu.com/?error_uri=%2Foauth2%2Fauthorize&error=access_denied&error_description=user%20denied%20your%20request.&error_code=21330
        // 点击授权:会以code=开头 http://www.baidu.com/?code=a61c2d7ec7b8c4c9c1962bf4354354c5
        // 获取url中?后面的内容: URL.query就是问号后面的东西
        if let query = request.URL?.query {
        // 能到这里面来,表示query有值
            let codeString = "code="
        // 判断query是否以code=开头
            if query.hasPrefix(codeString) {
                // 截取code的值
                // String和NSString之间的转换是不需要添加as? 或 as! 直接使用as
                let queryNS = query as NSString
                let code = queryNS.substringFromIndex(codeString.characters.count)
                print("code: \(code)")
                // 调用当前类的loadAccessToken
                self.loadAccessToken(code)
            }else {
            self.close()
            }
        
        }else {
         print("query没有值")
        }
        return false
    }
    func loadAccessToken(code: String) {
    // 调用网络工具类加载AccessToken
       HOPNetworkTools.sharedInstance.loadAccessToken(code) { (result, error) -> () in
        // 判断网络请求是否有错误
        // error == nil 模拟失败
        if error != nil {
        print("error: \(error)")
        SVProgressHUD.showInfoWithStatus("您的网络不给力")
//             延迟1秒在关闭
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                //关闭授权控制器
                self.close()
            })
              return
        }
        // 能到下面来没有错误,获取到了AccessToken
         // 保存AccessToken
        // 字典转模型
       let userAccount = HOPuserAccount(dict: result!)
        // 保存对象到沙盒
        print("result: \(result)")
        userAccount.saveUserAccount()
        print("userAccount: \(userAccount)")
        }
       }
    
    override func loadView() {
        // 在这赋值的话,不需要添加约束,系统会自动添加约束
        view = webView
        //监听web View加载网页
        webView.delegate = self
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
// 加载登陆界面,url在CZNetworkTool准备好了
        let url = HOPNetworkTools.sharedInstance.oAuthURL()
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
        // 设置导航栏
        // 左边,填充
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFill")
        // 右边,取消
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.title = "登陆"
        
    }
    /// 填充账号密码
    func autoFill() {
    let js = "document.getElementById('userId').value='18114703054'; document.getElementById('passwd').value='196844xujikang';"
    webView.stringByEvaluatingJavaScriptFromString(js)
    }
    /// 关闭
    func close() {
    dismissViewControllerAnimated(true, completion: nil)
    }
    // MARK: - 懒加载
    // WebView
    private lazy var webView = UIWebView()
    
}
